Inside Solaris logo
The Cobb Group This article is reprinted from the August 1996 issue of  Inside Solaris, a monthly publication of The Cobb Group.

Click for a FREE issue!


A couple of ls tricks

By Al Alexander

On occasion I hear people report problems when they use the ls command with directory listings. Sometimes they get unexpected results. In this article, we offer two quick tips for using the ls command with directories - and getting the proper results.

List only the directories

At times you'll want to generate a list of all the directories contained within the current direc-tory. To be more precise, you want to see a listing of directories only - no other files.

There are several ways to do this. One quick way to generate a long list of all of the directories in the current directory is to use the command

ls -alL | grep `^d'

This line tells Solaris to create a list of all the files in the current directory. The -l switch tells ls to print the directory in long format. This is important because we're going to examine the first character in the permissions mask to decide whether the file is a directory. The grep command goes through the directory listing, line by line, and prints each line that begins with the letter d.

As you may remember from last month's article "Hard and Soft File Links," when you create a symbolic link to a directory, the ls -al command will show an l in the first position, rather than a d, even if it's a directory. The -L switch on ls tells ls that in the case of a symbolic link, to print the permissions for the file linked to, rather than the permissions for the link itself. This avoids confusion, since a symbolically linked directory will show a d in the first position, rather than an l.

You can see some of the output of this command, run in the root directory of my Solaris 2.5 computer, in Figure A. Please note that the /bin directory, which is implemented as a symbolic link to the /usr/bin directory, appears normal (i.e., with a d permission) here.


Figure A: With a little effort, you can generate a list of only the directories within any directory.

$ ls -alL | grep `^d'
drwxr-xr-x  30 root     root        1024 Jun 19 00:09 .
drwxr-xr-x  30 root     root        1024 Jun 19 00:09 ..
drwxr-xr-x  12 root     other        512 Jun 19 00:11 .dt
drwx------   6 root     other	      512 Apr  4 21:36 .fm
drwxr-xr-x   2 root     other        512 Apr  2 10:28 .wastebasket
drwx------   4 root     other        512 Apr 16 14:15 Mail
drwxr-xr-x   2 root     root         512 Apr  1 22:36 TT_DB
drwxr-xr-x   2 root     other        512 May 10 11:51 a
drwxrwxr-x   2 root     bin         7168 Apr 29 18:38 bin
drwxr-xr-x   3 root     nobody       512 Apr 24 09:14 cdrom
drwxrwxr-x  17 root     sys         3072 Jun 16 15:22 dev
drwxrwxr-x   5 root     sys          512 Apr  1 21:01 devices
drwxrwxr-x  25 root     sys         3072 Jun 19 10:20 etc

If you just want to print the names of the directories, but not the long display, simply add a cut command at the end of your command chain. Since ls prints the filenames at column 55 with the -l format, we can just add the command cut -c55- to remove all the other information on the line. Thus, our revised command looks like this:

ls -al | grep `^d' | cut -c55-

On the other hand, if you don't mind a forward slash at the end of each directory name, here's a simpler way to accomplish the same task:

ls -alp | grep `/$'

This command will print the same information. If you want only the directory names, omit the -l option for ls. The -p option of ls is the key to this method. It tells ls to put a slash (/) on the end of each filename spec-ifying a directory. Then you tell grep to print all lines that end with a slash.

Listing a directory's properties, but not its contents

Let's discuss another question we encounter frequently: How do you look at the permissions, ownership, or size of a directory, rather than its contents? Your first instinct might be to type

ls -l /etc

However, if you ponder it a bit, you'll notice that you're actually asking ls for a list of all the information about the files in the directory, rather than about the di-rectory. Users frequently forget about the -d option of the ls command, which tells ls to give us the permissions of the dir-ectory file itself, rather than to list the dir-ectory's contents. Thus, what you really need to type is

ls -ld /etc

Conclusion

As you can see, it's easy to get a list of all the subdirectories in a directory. As is often the case with UNIX, there is usually more than one way to do a job. It's also easy to forget things you don't do every day, so it's no wonder people forget the -d switch on the ls command.

 

[The Cobb Group Home Page]

Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.

Questions? Comments?